
#include <allegro.h>
#include "general.h"
#include<stdio.h>
#include<stdlib.h>
void chargement (perso *p,bonus *b, perso *e,bonus *b1)
{

p->img_right[0]=load_bitmap("./images/right0.bmp",NULL);
p->img_right[1]=load_bitmap("./images/right1.bmp",NULL);
p->img_right[2]=load_bitmap("./images/right2.bmp",NULL);
p->img_left[0]=load_bitmap("./images/left0.bmp",NULL);
p->img_left[1]=load_bitmap("./images/left1.bmp",NULL);
p->img_left[2]=load_bitmap("./images/left2.bmp",NULL);

e->img_right[0]=load_bitmap("./images/right00.bmp",NULL);
e->img_right[1]=load_bitmap("./images/right01.bmp",NULL);
e->img_right[2]=load_bitmap("./images/right02.bmp",NULL);
e->img_left[0]=load_bitmap("./images/left00.bmp",NULL);
e->img_left[1]=load_bitmap("./images/left01.bmp",NULL);
e->img_left[2]=load_bitmap("./images/left02.bmp",NULL);

b->bon= load_bitmap("./images/bonus.bmp", NULL);
b1->bon= load_bitmap("./images/bonus.bmp", NULL);
}

void initialisation (perso *p,bonus *b, perso *e,perso *e1, bonus *b1,bonus *b2,bonus *b3) 
{
p->x=70;
p->y=450 ;
p->direction=0;
p->power = 100;


e->x=4200;
e->y=550 ;
e->power = 100;
e->direction=1;
e->nv=0;
e->power = 100;

e1->x=1400;
e1->y=550 ;
e1->direction=1;
e1->power = 100;
e1->nv=0;
e1->power = 100;

b3->x=1300;
b3->y=490;
b3->n=0;

b2->x=1200;
b2->y=490;
b2->n=0;

b1->x=900;
b1->y=470;
b1->n=0;

b->x=450;
b->y=520;
b->n=0;
}


void son ( SAMPLE *sound)
{

set_volume(255, 255); //digital and music to max levels


 
play_sample(sound, 255, 128, 1000, 0);
if (!sound)
    {allegro_message("Error reading wav file"); }

}

void son1 ( SAMPLE *sound)
{

set_volume(150, 150); //digital and music to max levels


 
play_sample(sound, 150, 128, 1000, 0);
if (!sound)
    {allegro_message("Error reading wav file"); }

}





//collision_background_down(poz,sprite,Background);

int bounding_box_collision_ennemie(int sprite1_x, int sprite1_y, int sprite1_w, int sprite1_h, int
sprite2_x, int sprite2_y, int sprite2_w, int sprite2_h)
{
if ((sprite1_x > sprite2_x + sprite2_w - 1) || // is sprite1 on the right side of sprite2?
(sprite1_y > sprite2_y + sprite2_h - 1) || // is sprite1 under sprite2?
(sprite2_x > sprite1_x + sprite1_w - 1) || // is sprite2 on the right side of sprite11?
(sprite2_y > sprite1_y + sprite1_h - 1) || (sprite1_x < sprite2_x + sprite2_w - 1) || // is sprite1 on the right side of sprite2?
(sprite1_y < sprite2_y + sprite2_h - 1) || // is sprite1 under sprite2?
(sprite2_x < sprite1_x + sprite1_w - 1) || // is sprite2 on the right side of sprite11?
(sprite2_y < sprite1_y + sprite1_h - 1)) // is sprite2 under sprite11?
{
// no collision
return 0;
}else
// collision
return 1;
}

int bounding_box_collision(int sprite1_x, int sprite1_y, int sprite1_w, int sprite1_h, int
sprite2_x, int sprite2_y, int sprite2_w, int sprite2_h)
{
if ((sprite1_x > sprite2_x + sprite2_w - 1) || // is sprite1 on the right side of sprite2?
(sprite1_y > sprite2_y + sprite2_h - 1) || // is sprite1 under sprite2?
(sprite2_x > sprite1_x + sprite1_w - 1) || // is sprite2 on the right side of sprite11?
(sprite2_y > sprite1_y + sprite1_h - 1))
// no collision
{
return 0;
}else
// collision
return 1;
}





void jump_pirate(float *poz,int *test ,perso *p,BITMAP * Background)
{ 
	Sprite sprite;
sprite.x=p->x;
sprite.y=p->y;
sprite.w=p->img_right[0]->w;
sprite.h=p->img_right[0]->h;
if (collision_background_up(poz,sprite,Background) == 1)
(*test) =1;
if (collision_background_down(poz,sprite,Background) == 1)
(*test) =0;

	if (key[KEY_UP] && (*test)==0 && !collision_background_up(poz,sprite,Background)) {
          
		(p->y)-=12;

	}

}  

void gravity(float *y)
{   
	(*y)+=3;
}


void gravity_pirate(float *poz,perso *p,BITMAP * Background)
{
	Sprite sprite;
sprite.x=p->x;
sprite.y=p->y;
sprite.w=p->img_right[0]->w;
sprite.h=p->img_right[0]->h;

	if(collision_background_down(poz, sprite,Background)==0)
{ 
		gravity(&(p->y));

}

}
 


void move_pirate(float *poz,int *test ,perso *p,int *compteur,BITMAP * Background)
{
	jump_pirate(poz,test,p,Background);

	gravity_pirate(poz,p,Background);
}

int collision_background_up(float *poz,Sprite sprite,BITMAP* backgroundcol)
{       int x;
	int y;
	x=sprite.x + (sprite.w /2);
	y=sprite.y;
	
       
	if( get_pixel(backgroundcol,(*poz)+x,y)==255)
	return 1;
    else
    return 0; 

}
int collision_background_down(float *poz,Sprite sprite,BITMAP* backgroundcol)
{
	int x;
	int y;
	x=sprite.x + (sprite.w /2);
	y=sprite.y + sprite.h;


	if( get_pixel(backgroundcol,(*poz)+x,y)== 255)
		return (1); //collision with the middle down side of the sprite
else
	return (0); //no collision

}



int collision_background_left(float poz,perso p,BITMAP* BackgroundCol)
{
	float x;
	float y;
	x=p.x;
	y=p.y + (p.img_right[0]->h /2);

	if( get_pixel(BackgroundCol,poz +x,y)== 255|| get_pixel(BackgroundCol,poz +x,y+(p.img_right[0]->h /2)-20)== 255)
		return (1); //collision with the middle left side of the sprite
else
	return (0); //no collision
}

int collision_background_right(float poz ,perso p,BITMAP* BackgroundCol)
{
	float x;
	float y;
	x=p.x + p.img_right[0]->w;
	y=p.y + (p.img_right[0]->h /2);

	if( get_pixel(BackgroundCol,poz +x,y)== 255 || get_pixel(BackgroundCol,poz +x,y+(p.img_right[0]->h /2)-20)== 255)
		return (1); //collision with the middle left side of the sprite
else
	return (0); //no collision
}


int get_pixel(BITMAP* backgroundcol,int x,int y)
{
	int tmp;
	tmp=getpixel(backgroundcol,x,y);
	return((getr(tmp) + getb(tmp) + getg(tmp)) / 3);
}

void enregistrer(FILE *F,perso *p,perso *e,perso *e1,BITMAP *Background,BITMAP *backdgroundcol,bonus *b,bonus *b1,bonus *b2,bonus *b3,int somme_bonus,float poz,int test)
{F = fopen("enregistrer.txt","w");

fprintf (F, "%0.0f\n%0.0f\n%d\n%d\n%0.0f\n%0.0f\n%d\n%d\n%0.0f\n%0.0f\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n%0.0f\n%d",p->x,p->y,p->direction,p->power,p->x,e->y,e->direction,e->power,e1->x,e1->y,e1->direction,e1->power,b->x,b->y,b->n,b1->x,b1->y,b1->n,b2->x,b2->y,b2->n,b3->x,b3->y,b3->n,somme_bonus,poz,test);

fclose(F);}



void charger_partie(FILE *F,perso *p,perso *e,perso *e1,BITMAP *Background,BITMAP *backdgroundcol,bonus *b,bonus *b1,bonus *b2,bonus *b3,int *somme_bonus,float *poz,int *test)
{

F=fopen("enregistrer.txt","r");


while(fscanf (F, "%f\n%f\n%d \n%d \n%f \n%f\n%d \n%d\n%f \n%f \n%d \n%d\n%d \n%d \n%d \n%d \n%d \n%d \n%d\n%d \n%d\n%d \n %d \n %d\n %d\n %f \n %d",&p->x,&p->y,&p->direction,&p->power,&p->x,&e->y,&e->direction,&e->power,&e1->x,&e1->y,&e1->direction,&e1->power,&b->x,&b->y,&b->n,&b1->x,&b1->y,&b1->n,&b2->x,&b2->y,&b2->n,&b3->x,&b3->y,&b3->n,somme_bonus,poz,test)!=EOF)
{} 



fclose(F);

}















